package com.books4sale.feed.atom.builder;

import java.util.Date;

import org.restlet.data.CharacterSet;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.ext.atom.Content;
import org.restlet.ext.atom.Entry;
import org.restlet.ext.atom.Link;
import org.restlet.ext.atom.Person;
import org.restlet.ext.atom.Relation;
import org.restlet.ext.atom.Text;
import org.restlet.representation.StringRepresentation;

import com.books4sale.admin.dto.Book;
import com.books4sale.feed.atom.MyFeed;

public class AtomBuilder {

	private static AtomBuilder instance = new AtomBuilder();

	public AtomBuilder() {
	}

	public static AtomBuilder get() {
		return instance;
	}

	/**
	 * Build atom entry for a given book
	 * 
	 * @param book
	 * @return atom entry contains full book info
	 */
	public Entry buildBookEntry(Book book) {
		Entry bookEntry = new Entry();

		Text entryTitle = new Text(MediaType.TEXT_ALL);
		entryTitle.setContent(book.getBookName());

		String entrySummary = book.getBookName() + " / "
				+ book.getBookCollage();
		// Text entrySummary = new Text(MediaType.TEXT_ALL);
		// entrySummary.setContent(book.getBookName() + " / " +
		// book.getBookCollage());

		Content entryContent = new Content();
//		entryContent
//				.setInlineContent(new StringRepresentation(
//						"<div style='text-align: right;color:blue' dir='rtl'>"
//								+ "<b style='color:black; text-decoration:underline;'>المادة: </b> "
//								+ book.getBookSubject()
//								+ "</br>"
//								+ "<b style='color:black; text-decoration:underline;'>الكليّة: </b> "
//								+ book.getBookSubject()
//								+ "</br>"
//								+ "<b style='color:black; text-decoration:underline;'>السعر: </b> "
//								+ book.getBookPrice()
//								+ " شيكل"
//								+ "</br>"
//								+ "<b style='color:black; text-decoration:underline;'>اسم المحل: </b> "
//								+ book.getProfile().getShopName()
//								+ "</br>"
//								+ "<b style='color:black; text-decoration:underline;'>العنوان: </b> "
//								+ book.getProfile().getShopAddress() + "/ "
//								+ book.getProfile().getShopCity() + "</br>"
//								+ "</div>", MediaType.TEXT_HTML));
		entryContent.setInlineContent(new StringRepresentation(
				"الموضوع هنا بالعربيشسبتمشسنبت م سميبت كشسمنتب منت بشمستيب كمنسيتبكشمسنيتبشسمنيبتش كشسنيتب سمتب مسنتب شسمنبت شسكمينبت شسيمبن شسيمبت منشتسيكبمنت شسيمبت شسمينبت شسمنيبت شسمنيتب كمشنستيب كشمنسيتب كمشنستيب شكمنستيب شمنستيب شنتسيب كشمسيب شم", MediaType.TEXT_HTML,Language.valueOf("ar"),CharacterSet.UTF_8));

		String entryId = "book_" + book.getDate().getTime();

		Link link = new Link();
		link.setRel(Relation.ALTERNATE);
		link.setHref(new Reference("http://new.sell-ur-book.com/"
				+ book.getId().getId()));
		link.setType(MediaType.APPLICATION_XHTML_XML);

		Date published = book.getDate();

		Date updated = book.getDate();

		bookEntry.setTitle(entryTitle);
		bookEntry.setSummary(entrySummary);
		bookEntry.setContent(entryContent);
		bookEntry.setId(entryId);
		bookEntry.getLinks().add(link);
		bookEntry.setUpdated(updated);
		bookEntry.setPublished(published);

		// Text title = new Text(book.getBookCollage() + " / "
		// + book.getBookSubject() + " / " + book.getBookName() + " / "
		// + book.getBookPrice() + " شيكل");
		// Link entryLink = new Link();
		// Content entryContent = new Content();
		//
		// entryLink.setHref(new Reference("#"));
		// entryLink.setRel(Relation.SELF);
		// entryLink.setTitle("اقرأ المزيد");
		//
		// String id = book.getId().getId() + "";
		// Date updated = book.getDate();
		//
		// entryContent.setInlineContent(new StringRepresentation("<div>"
		// + book.getProfile().getShopName() + " / "
		// + book.getProfile().getShopAddress() + " / "
		// + book.getProfile().getShopCity() + "</div>",
		// MediaType.TEXT_HTML));

		// entryContent
		// .setInlineContent(new StringRepresentation(
		// "<div style='text-align: right;color:blue' dir='rtl'>"
		// + "<b style='color:black; text-decoration:underline;'>المادة: </b> "
		// + book.getBookSubject()
		// + "</br>"
		// + "<b style='color:black; text-decoration:underline;'>الكليّة: </b> "
		// + book.getBookSubject()
		// + "</br>"
		// + "<b style='color:black; text-decoration:underline;'>السعر: </b> "
		// + book.getBookPrice()
		// + " شيكل"
		// + "</br>"
		// +
		// "<b style='color:black; text-decoration:underline;'>اسم المحل: </b> "
		// + book.getProfile().getShopName()
		// + "</br>"
		// + "<b style='color:black; text-decoration:underline;'>العنوان: </b> "
		// + book.getProfile().getShopAddress() + "/ "
		// + book.getProfile().getShopCity() + "</br>"
		// + "</div>"));

		// bookEntry.setTitle(title);
		// bookEntry.getLinks().add(entryLink);
		// bookEntry.setContent(entryContent);
		// bookEntry.setId(id);
		// bookEntry.setUpdated(updated);

		return bookEntry;
	}

	public MyFeed buildFeed() {
		MyFeed atomFeed = new MyFeed();

		Text feedTitle = new Text(MediaType.TEXT_ALL);
		feedTitle.setContent("Books4Sale");

		Text feedSubtitle = new Text(MediaType.TEXT_ALL);
		feedSubtitle.setContent("Books4Sale");

		String feedId = "b4s:mainFeed";

		Link feedLink1 = new Link(new Reference("http://new.sell-ur-book.com"),
				Relation.SELF, MediaType.TEXT_HTML);

		Link feedLink2 = new Link(new Reference("http://new.sell-ur-book.com"),
				Relation.ALTERNATE, MediaType.APPLICATION_ATOM_XML);

		Person feedAuthor = new Person();
		feedAuthor.setName("info@sell-ur-books.com");
		feedAuthor.setEmail("info@sell-ur-book.com");
		feedAuthor.setUri(new Reference("http://new.sell-ur-book.com"));

		Text copyRights = new Text(MediaType.TEXT_ALL);
		copyRights.setContent("All copyrights reserved");

		Date updated = new Date();

		atomFeed.setTitle(feedTitle);
		atomFeed.setSubtitle(feedSubtitle);
		atomFeed.setId(feedId);
		atomFeed.getLinks().add(feedLink1);
		atomFeed.getLinks().add(feedLink2);
		atomFeed.getAuthors().add(feedAuthor);
		atomFeed.setRights(copyRights);
		atomFeed.setUpdated(updated);

		// feedAuthor.setName("Admin");
		// feedAuthor.setEmail("info@sell-ur-book.com");
		// atomFeed.getAuthors().add(feedAuthor);
		//
		// feedTitle.setContent("Books 4 Sale");
		// atomFeed.setTitle(feedTitle);
		//
		// feedId = "b4s/abf";
		// atomFeed.setId(feedId);
		//
		// feedLink.setRel(Relation.SELF);
		// feedLink.setHref(new Reference("http://new.sell-ur-book.com"));
		// feedLink.setTitle("Books4Sale");
		// feedLink.setType(MediaType.TEXT_XML);
		// atomFeed.getLinks().add(feedLink);

		return atomFeed;
	}
}
